﻿<%@ Page Language="C#" MasterPageFile="~/Admin/admin.master" AutoEventWireup="true"
    CodeFile="RequiredAdjustment.aspx.cs" Inherits="Admin_RequiredAdjustment" Title="" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <table border="0" cellpadding="3" cellspacing="1" runat="server" id="tbl">
        <tr>
            <td class="literal">
                <asp:Label ID="lbldataAdjustmentId" runat="server" Text="Approve Remark:" AssociatedControlID="txtApproveRemark" />
            </td>
            <td>
                <asp:TextBox ID="txtApproveRemark" runat="server" Text="" CssClass="text-input"></asp:TextBox>
            </td>
        </tr>
        <tr class="alt-row">
            <td colspan="2">
                <asp:Button runat="server" ID="btnApprove" Text="Approve All" CssClass="button" OnClick="btnApprove_Click"
                    OnClientClick="return confirm('Are you sure want to approve all?');"></asp:Button>
            </td>
        </tr>
    </table>
    <br />
    <data:GridViewSearchPanel ID="GridViewSearchPanel1" runat="server" GridViewControlID="GridView1" />
    <br />
    <data:EntityGridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
        DataSourceID="ViewRequiredAdjustmentDataSource" DataKeyNames="RequirementId"
        AllowMultiColumnSorting="False" DefaultSortColumnName="DrugId" DefaultSortDirection="Ascending"
        ExcelExportFileName="Export_AdjustmentDetail.xls" AllowExportToExcel="False"
        AllowPaging="True" AllowSorting="True" ExportToExcelText="Excel" PageSelectorPageSizeInterval="20"
        RecordsCount="0" ShowGridOnEmptyData="False" OnRowCommand="GridView1_RowCommand">
        <Columns>
            <asp:TemplateField HeaderText="Drug Id" SortExpression="[DrugId]">
                <ItemTemplate>
                    <asp:HiddenField ID="hdfRequirementId" runat="server" Visible="false" Value='<%#Eval("RequirementId") %>' />
                    <asp:Label ID="lblDrugId" runat="server" Text='<%# Eval("DrugId") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="DrugName" HeaderText="Drug Name" SortExpression="[DrugName]"
                ReadOnly="True" />
            <asp:TemplateField HeaderText="Current Quantity" SortExpression="[Quantity]">
                <ItemTemplate>
                    <asp:Label ID="lblQuantity" runat="server" Text='<%# Eval("Quantity","{0:#,##0}") %>'></asp:Label>
                    <asp:HiddenField ID="hdfQuantity" runat="server" Value='<%# Eval("Quantity") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Adjust Quantity" SortExpression="[AdjustQuantity]">
                <ItemTemplate>
                    <asp:TextBox ID="txtAdjustQuantity" runat="server" Text='<%# Eval("AdjustQuantity") %>'
                        Width="40"></asp:TextBox>
                    <asp:HiddenField ID="hdfAdjustQuantity" runat="server" Value='<%# Eval("AdjustQuantity") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Final Quantity" SortExpression="[FinalQuantity]">
                <ItemTemplate>
                    <asp:Label ID="lblFinalQuantity" runat="server" Text='<%# Eval("FinalQuantity","{0:#,##0}") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Remark" SortExpression="[Remark]">
                <ItemTemplate>
                    <asp:TextBox ID="txtRemark" runat="server" Text='<%# Eval("Remark") %>' Width="120"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button runat="server" ID="btnSave" CommandName="CustomSave" CommandArgument='<%#Eval("RequirementId") %>'
                        Text="Save" CssClass="button"></asp:Button>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EmptyDataTemplate>
            <b>No Adjustment Details Found!</b>
        </EmptyDataTemplate>
    </data:EntityGridView>
    <br />
    <asp:Button runat="server" ID="btnSaveAll" Text="Save All" CssClass="button" OnClick="btnSaveAll_Click">
    </asp:Button>
    <data:ViewRequiredAdjustmentDataSource ID="ViewRequiredAdjustmentDataSource" runat="server"
        SelectMethod="GetPaged" EnablePaging="true" EnableSorting="true">
        <Parameters>
            <data:CustomParameter Name="WhereClause" Value="" ConvertEmptyStringToNull="false" />
            <data:CustomParameter Name="OrderByClause" Value="" ConvertEmptyStringToNull="false" />
            <asp:ControlParameter Name="PageIndex" ControlID="GridView1" PropertyName="PageIndex"
                Type="Int32" />
            <asp:ControlParameter Name="PageSize" ControlID="GridView1" PropertyName="PageSize"
                Type="Int32" />
            <data:CustomParameter Name="RecordCount" Value="0" Type="Int32" />
        </Parameters>
    </data:ViewRequiredAdjustmentDataSource>
    <br />

    <script language="javascript" type="text/javascript">
        $(document).ready(function() {
            $("[id$=txtAdjustQuantity]").focus(function() {
                $(this).select();
            }).change(function() {
                var parent = ($(this).parent()).parent();
                var hdfQuantity = $(parent).find("[id$=hdfQuantity]");
                var hdfAdjustQuantity = $(parent).find("[id$=hdfAdjustQuantity]");
                var lblFinalQuantity = $(parent).find("[id$=lblFinalQuantity]");
                $(this).val($.trim($(this).val()));

                if (isNaN($(this).val())) {
                    $(this).val(hdfAdjustQuantity.val());
                } else {
                    $(this).val(Number($(this).val()));
                }

                var quantity = Number($(hdfQuantity).val());
                var adjustQuantity = Number($(this).val());
                var finalQuantity = quantity + adjustQuantity;

                if (finalQuantity < 0) {
                    alert("Final Quantity must be greater than 0.");
                    $(this).val(hdfAdjustQuantity.val());
                    return;
                }

                hdfAdjustQuantity.val(adjustQuantity);

                $(lblFinalQuantity).text(finalQuantity);
                $(lblFinalQuantity).prettynumber({delimiter : ','});
            });
        });
    </script>

</asp:Content>
